﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using PSE.Updater.Server.Engine;
using System.Configuration;
using System.ServiceModel;
using PSE.Updater.Server.Service;
using Provider = PSE.Updater.Server.Engine;

namespace PSE.Updater.Server
{
    partial class UpdateService : ServiceBase
    {
        private ServiceHost _gatewayServiceHoster;
        private ServiceHost _downloadServiceHoster;
        //private ServiceHost _updatesRegistryProductFoldersService;

        public UpdateService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Threading.Thread.Sleep(10.SecondsToMilliseconds());

            DebugLog.Write("Inicializando o Updater");

            //iniciar o componente de monitoracao do repositorio de atualizacoes
            Provider.UpdateService.Instance.Initialize();

            //iniciar o GateWay
            _gatewayServiceHoster = new ServiceHost(typeof(UpdatesDiscoveryService));
            _gatewayServiceHoster.Open();

            _downloadServiceHoster = new ServiceHost(typeof(UpdatesDownloadService));
            _downloadServiceHoster.Open();

            //_updatesRegistryProductFoldersService = new ServiceHost(typeof(UpdatesRegistryProductFoldersService));
            //_updatesRegistryProductFoldersService.Open();

            // TODO: Add code here to start your service.
        }

        protected override void OnStop()
        {
            if (_gatewayServiceHoster != null)
                _gatewayServiceHoster.Close();

            if (_downloadServiceHoster != null)
                _downloadServiceHoster.Close();

            //if (_updatesRegistryProductFoldersService != null)
            //    _updatesRegistryProductFoldersService.Close();

            Provider.UpdateService.Instance.Terminate();
        }
    }
}
